package com.wxb.hello.activemq.demop2p;

import com.wxb.hello.activemq.repository.MessageObject;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

/**
 * describe:
 *
 * @author : 王校兵
 * @version : v1.0
 * @time : 2017/7/27  9:52
 */
public class JMSConsumer {

    private static final String USERNAME = ActiveMQConnection.DEFAULT_USER;//默认连接用户名
    private static final String PASSWORD = ActiveMQConnection.DEFAULT_PASSWORD;//默认连接密码
    private static final String BROKEURL = "tcp://192.168.32.130:61616";//默认连接地址

    public static void main(String[] args) {
        ConnectionFactory connectionFactory;//连接工厂
        Connection connection = null;//连接

        Session session;//会话 接受或者发送消息的线程
        Destination destination;//消息的目的地

        MessageConsumer messageConsumer;//消息的消费者

        //实例化连接工厂
        connectionFactory = new ActiveMQConnectionFactory(JMSConsumer.USERNAME, JMSConsumer.PASSWORD, JMSConsumer.BROKEURL);

        try {
            //通过连接工厂获取连接
            connection = connectionFactory.createConnection();
            //启动连接
            connection.start();
            //创建session
            session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            //创建一个连接HelloWorld的消息队列
            destination = session.createQueue("HelloWorld?consumer.prefetchSize=10000");
            //创建消息消费者
            messageConsumer = session.createConsumer(destination);

            //实现一个消息的监听器
            //实现这个监听器后，以后只要有消息，就会通过这个监听器接收到
            messageConsumer.setMessageListener(new MessageListener() {
                @Override
                public void onMessage(Message msg) {
                    try {
                        //获取到接收的数据
                        if (msg instanceof TextMessage) {//接收text消息
                            System.out.println(((TextMessage) msg).getText());
                        } else if (msg instanceof MapMessage) {//接收map消息
                            System.out.println(((MapMessage) msg).getString("name"));
                            System.out.println(((MapMessage) msg).getString("sal"));
                        } else if (msg instanceof ObjectMessage) {//接收序列号消息

                            MessageObject messageObject = (MessageObject) ((ObjectMessage) msg).getObject();
                            System.out.println(messageObject.toString());

                        } else if (msg instanceof StreamMessage) {  //接收流消息
                            System.out.println(((StreamMessage) msg).readString());
                        } else if (msg instanceof BytesMessage) {   //接收字节消息
                            byte[] bs = new byte[1024];
                            BytesMessage message = (BytesMessage) msg;
                            while (message.readBytes(bs) != -1) {
                                System.out.println(new String(bs));
                            }
                        }
                    } catch (JMSException e) {
                        e.printStackTrace();
                    }
                }
            });

        } catch (JMSException e) {
            e.printStackTrace();
        }
    }
}